
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{php}
date_default_timezone_set('Asia/Shanghai');
{/php}
<img src="" id="img">
<a href="javascript:;" onclick="uplaodwximg()">微信上传图片</a>
<div id ="pic">

</div>
</body>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://res2.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script>
    function uplaodwximg() {
        $.ajax({
            url: "{:url('Account/test')}",//请求地址
            type: 'post',//post请求
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            data: {
                'url': location.href.split('#')[0]//将第一个#号前的地址传递
            },
            success: function (data) {
                wx.config({
//                    debug: true,
                    appId: data.data.appId,
                    timestamp: data.data.timestamp,
                    nonceStr: data.data.nonceStr,
                    signature: data.data.signature,
                    jsApiList: ['uploadImage', 'chooseImage', 'downloadImage','previewImage']
                });
                wx.ready(function () {
                    wx.chooseImage({
                        count: 1, // 默认9
                        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                        success: function (res) {
                            var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
                            $('#img').attr('src', localIds[0]);
                            // 上传照片
                            wx.uploadImage({
                                localId: '' + localIds[0],
                                isShowProgressTips: 1,
                                success: function (res) {
                                    serverId = res.serverId; alert(serverId)
                                    $.ajax({
                                        url:'{:url("wximg")}',
                                        type: "post",
                                        dataType: "json",
                                        data: {
                                            localData: serverId,
                                        },
                                        success: function (data) {
                                            alert(data.src)
                                        },
                                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                                            alert(errorThrown);
                                        },
                                    });
                                }
                            });
                        }
                    });
                });
                wx.error(function (e) {
                    alert(e)
                })

            }
        })
    }





</script>
</html>